package com.example.zhengchaojun.arcgis.basicesTest.adapter;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.esri.arcgisruntime.concurrent.ListenableFuture;
import com.esri.arcgisruntime.data.Feature;
import com.esri.arcgisruntime.layers.FeatureLayer;
import com.esri.arcgisruntime.layers.Layer;
import com.esri.arcgisruntime.symbology.Symbol;
import com.example.zhengchaojun.arcgis.R;
import com.example.zhengchaojun.arcgis.baseadapter.ListViewBaseAdapter;

import java.util.List;
import java.util.concurrent.ExecutionException;

/**
 * 类描述：图例管理
 * 作者：郑朝军 on 2018/5/31
 * 邮箱：1250393285@qq.com
 * 公司：武汉智博创享科技有限公司
 * 修改人：郑朝军 on 2018/5/31
 * 修改备注：
 */
public class LeandListviewAdapter extends ListViewBaseAdapter<Layer>
{
    public LeandListviewAdapter(Activity activity, List<Layer> list)
    {
        super(activity, list);
    }

    public View getView(final int position, View convertView, ViewGroup parent)
    {
        convertView = getItemView(convertView, R.layout.widget_view_layer_managet_legent_item, parent);
        final ImageView imageView = getView(convertView, R.id.widget_view_layer_managet_legent_item_img);
        final TextView textView = getView(convertView, R.id.widget_view_layer_managet_legent_item_txtName);

        final Layer layers = getItem(position);
        textView.setText(layers.getName());

        
        //仅获取当前显示的layer
        FeatureLayer layer = null;
        int indexPositon = 0;//计数
        List list = getList();
        for (int i = 0; i < list.size(); i++)
        {
            Layer layerTpl = (Layer) list.get(i);
            if (layerTpl.isVisible())
            {
                if (indexPositon == position)
                {
                    layer = (FeatureLayer) layerTpl;
                }
                indexPositon++;
            }
        }
        //要素模板
        Feature feature = layer.getFeatureTable().createFeature();
        Symbol symbol = layer.getRenderer().getSymbol(feature);
        ListenableFuture<Bitmap> bitmap = symbol.createSwatchAsync(activity, Color.WHITE);
        try
        {
            imageView.setImageBitmap(bitmap.get());
        }
        catch (InterruptedException e)
        {
            e.printStackTrace();
        }
        catch (ExecutionException e)
        {
            e.printStackTrace();
        }
        return convertView;
    }
}
